#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#define PI 3.1415926
typedef enum{
	ERROR=0,
	OK=1
} Status;

typedef struct{
	float r;
	float i;
	float abv;
} Complex;
//复数的加法
Complex add(Complex z1,Complex z2)
{
	Complex z;
	z.r=z1.r+z2.r;
	z.i=z1.i+z2.i;
	return z;
}
//复数乘纯复指数
Complex multi(Complex z1,float z2)
{
	Complex z;
	z.r=z1.r*cos(z2)-z1.i*sin(z2);
	z.i=z1.r*sin(z2)+z1.i*cos(z2);
	return z;
}
//实数乘纯副指数
Complex real(float r1,float r2)
{
	Complex z;
	z.i=0;
	z.r=r1;
	z=multi(z,r2);
	return z;
}

Status IDFT(int nw,int nt,float dw,float **rick,Complex *amp)
{
	int iw,it;
	*rick=(float *)malloc(sizeof(float)*nt);
	for(it=0;it<nt;it++) (*rick)[it]=0;
	for(it=0;it<nt;it++)
		for(iw=0;iw<nw;iw++)
		{
			(*rick)[it]+=1./nw*multi(amp[iw],2.*PI/nw*iw*it).r;
		}
	return OK;
}

int main()
{
	int iw,it;
	char fin[100],fout[100];
	int nw,nt;
	float dt,*rick,dw;
	Complex *amp;
	FILE *fp;
	fp=fopen("../par/ifourier.txt","r");
	fscanf(fp,"fin=%s\n",fin);
	fscanf(fp,"nt=%d\n",&nt);
	fscanf(fp,"nw=%d\n",&nw);
	fscanf(fp,"dt=%f\n",&dt);
	fscanf(fp,"fout=%s\n",fout);
	fclose(fp);
	fp=fopen(fin,"r");
	amp=(Complex *)malloc(sizeof(Complex)*nw);
	float temp;
	for(iw=0;iw<nw;iw++)
		fscanf(fp,"%f\t%f\t%f\t%f\n",&temp,&amp[iw].r,&amp[iw].i,&amp[iw].abv);
	fclose(fp);
	IDFT(nw,nt,dw,&rick,amp);
	fp=fopen(fout,"w");
	if(!fp)
		return -1;
	for(it=0;it<nt;it++)
	{
		fprintf(fp,"%f\t%f\n",it*dt,rick[it]);
	}
	fclose(fp);
	free(amp);
	free(rick);
	return 0;
}





































